package multithread.Semaphore;

import java.util.concurrent.Semaphore;

public class Demo {
	
	public void method (Semaphore semaphore) {
		try {
			System.out.println(Thread.currentThread().getName() + " is enter lock ...");
			semaphore.acquire();//获取锁,如果有就不等待，如果没有流了就等待了，
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + " is run ...");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		semaphore.release(); //释放锁
	}
	
	
	public static void main(String[] args) {
		Demo d = new Demo();
		Semaphore semaphore = new Semaphore(2);
		while(true) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					d.method(semaphore);
				}
			}).start();
		}
		
		
	}

}
